package com.isnmp.common.snmp.service;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.isnmp.common.base.MyException;
import com.isnmp.common.snmp.SNMPFactory;
import com.isnmp.common.snmp.SNMPTarget;
import com.isnmp.common.snmp.impl.SNMPUtils;
import com.isnmp.common.snmp.mib.Dot1dBasePortEntry;
import com.isnmp.common.snmp.mib.Dot1dTpFdbEntry;
import com.isnmp.common.snmp.mib.MibIfEntry;
import com.isnmp.common.snmp.mib.MibMacIP;

public class SNMPLogic {
    public static Vector<PCInfoOfSwitch> getPCInfoOfSwitch(SNMPTarget paramSNMPTarget) throws MyException {
        Vector localVector = new Vector();
        try {
            List localList1 = SNMPFactory.getSNMPAPI().getAllTableData(Dot1dTpFdbEntry.class, paramSNMPTarget);
            List localList2 = SNMPFactory.getSNMPAPI().getAllTableData(MibMacIP.class, paramSNMPTarget);
            Iterator localIterator1 = localList1.iterator();
            while (localIterator1.hasNext()) {
                Dot1dTpFdbEntry localDot1dTpFdbEntry = (Dot1dTpFdbEntry) localIterator1.next();
                PCInfoOfSwitch localPCInfoOfSwitch = new PCInfoOfSwitch();
                localPCInfoOfSwitch.setSwitchip(paramSNMPTarget.nodeIP);
                localPCInfoOfSwitch.setSwitchPortNum(localDot1dTpFdbEntry.getDot1dTpFdbPort());
                localPCInfoOfSwitch.setPcMac(SNMPUtils.formatDispayMacAddress(localDot1dTpFdbEntry.getDot1dTpFdbAddress()));
                System.out.println("New PC INFO,SwitchIP:" + paramSNMPTarget.nodeIP + ",PortNum:" + localPCInfoOfSwitch.getSwitchPortNum() + ",MAC:" + localPCInfoOfSwitch.getPcMac());
                Iterator localIterator2 = localList2.iterator();
                while (localIterator2.hasNext()) {
                    MibMacIP localMibMacIP = (MibMacIP) localIterator2.next();
                    String str1 = SNMPUtils.formatDispayMacAddress(localMibMacIP.getIpNetToMediaPhysAddress());
                    if (localPCInfoOfSwitch.getPcMac().equalsIgnoreCase(str1)) {
                        localPCInfoOfSwitch.setPcIP(localMibMacIP.getIpNetToMediaNetAddress());
                        try {
                            InetAddress localInetAddress = InetAddress.getByName(localPCInfoOfSwitch.getPcIP());
                            String str2 = localInetAddress.getHostName();
                            localPCInfoOfSwitch.setPcName(str2);
                        } catch (Exception localException2) {
                        }
                        System.out.println("Fined MAC-IP,PCMAC:" + localPCInfoOfSwitch.getPcMac() + ",PCIP:" + localPCInfoOfSwitch.getPcIP());
                        localVector.add(localPCInfoOfSwitch);
                    }
                }
            }
            return localVector;
        } catch (Exception localException1) {
            System.err.println("Get PC Of Switch err,IP:" + paramSNMPTarget.nodeIP + ",err:" + localException1.getMessage());
            localException1.printStackTrace();
            throw new MyException(localException1.getMessage());
        }
    }

    public static void setSwitchPortStats(SNMPTarget paramSNMPTarget, int paramInt, boolean paramBoolean) {
        int i = getIndexofPort(paramSNMPTarget, paramInt);
        try {
            MibIfEntry localMibIfEntry = new MibIfEntry();
            localMibIfEntry.setIfIndex(i);
            if (paramBoolean)
                localMibIfEntry.setIfAdminStatus(1);
            else
                localMibIfEntry.setIfAdminStatus(2);
            SNMPFactory.getSNMPAPI().update(localMibIfEntry, paramSNMPTarget);
            System.out.println("Set Port stats OK.IP:" + paramSNMPTarget.nodeIP + ",portNum:" + paramInt + ",InterFace Num:" + i + ",stats:" + paramBoolean);
        } catch (Exception localException) {
            System.err.println("Set Port stats Erro!IP:" + paramSNMPTarget.nodeIP + ",portNum:" + paramInt + ",InterFace Num:" + i + ",stats:" + paramBoolean);
            System.err.println("set port stats err:" + localException.getMessage());
            localException.printStackTrace();
        }
    }

    private static int getIndexofPort(SNMPTarget paramSNMPTarget, int paramInt) {
        try {
            List localList = SNMPFactory.getSNMPAPI().getAllTableData(Dot1dBasePortEntry.class, paramSNMPTarget);
            Iterator localIterator = localList.iterator();
            while (localIterator.hasNext()) {
                Dot1dBasePortEntry localDot1dBasePortEntry = (Dot1dBasePortEntry) localIterator.next();
                if (localDot1dBasePortEntry.getDot1dBasePort() == paramInt) {
                    System.out.println("Find InterFace Port,IP: " + paramSNMPTarget.nodeIP + " ,Port:" + paramInt + " ,InterfaceNum:" + localDot1dBasePortEntry.getDot1dBasePort());
                    return localDot1dBasePortEntry.getDot1dBasePortIfIndex();
                }
            }
            System.err.println("Can't Find InterFace Port Num of " + paramSNMPTarget.nodeIP + " Port:" + paramInt + " ");
            return paramInt;
        } catch (Exception localException) {
            System.err.println("Find InterFace Port Num of " + paramSNMPTarget.nodeIP + " Port:" + paramInt + " ;Erro:" + localException.getMessage());
            localException.printStackTrace();
        }
        return paramInt;
    }

    public static void main(String[] paramArrayOfString) {
        try {
            SNMPFactory.init(1, null, 0);
            SNMPTarget localSNMPTarget = new SNMPTarget();
            localSNMPTarget.nodeIP = "192.168.0.102";
            localSNMPTarget.readCommunity = "public";
            Vector localVector = getPCInfoOfSwitch(localSNMPTarget);
            Iterator localIterator = localVector.iterator();
            while (localIterator.hasNext()) {
                PCInfoOfSwitch localPCInfoOfSwitch = (PCInfoOfSwitch) localIterator.next();
                System.out.println(localPCInfoOfSwitch.getPcMac() + "," + localPCInfoOfSwitch.getSwitchPortNum() + "," + localPCInfoOfSwitch.getPcIP());
            }
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }
}